/**
 * Notes: 后台管理模块业务基类
 * Date: 2021-03-15 07:48:00 
 * Ver : CCMiniCloud Framework 2.0.8 ALL RIGHTS RESERVED BY cclinux0730 (wechat)
 */

const BaseService = require('./base_service.js');

const timeUtil = require('../../helpers/time_util.js');
const appCode = require('../../../framework/core/app_code.js');

const config = require('../../../config/config.js');

const { AdminModel, LogModel } = require('../config/models_config');
const LogCache = require('../../helpers/log_cache.js');

class BaseAdminService extends BaseService {


	/** 是否管理员 */
	async isAdmin(token) {

		if (config.IS_DEMO) { // 演示版本
			let admin = {};
			admin.ADMIN_NAME = 'demo-admin';
			admin.ADMIN_DESC = '体验用户';
			admin.ADMIN_ID = '1';
			admin.ADMIN_PHONE = '13900000000';
			admin.ADMIN_LOGIN_CNT = 0;
			admin.ADMIN_LOGIN_TIME = '';
			admin.ADMIN_TYPE = 0;
			admin.ADMIN_STATUS = 1;
			return admin;
		}

		let where = {
			ADMIN_TOKEN: token,
			ADMIN_TOKEN_TIME: ['>', timeUtil.time() - config.ADMIN_LOGIN_EXPIRE * 1000], // token有效时间
			ADMIN_STATUS: 1,
		}
		
		// 使用静态方法调用getOne
		let admin = await AdminModel.getOne(where, 'ADMIN_ID,ADMIN_PHONE,ADMIN_NAME,ADMIN_TYPE,ADMIN_DESC');
		if (!admin)
			this.AppError('管理员不存在', appCode.ADMIN_ERROR);

		return admin;
	}

	/** 是否超级管理员 */
	async isSuperAdmin(token) {

		let where = {
			ADMIN_TOKEN: token,
			ADMIN_TOKEN_TIME: ['>', timeUtil.time() - config.ADMIN_LOGIN_EXPIRE * 1000], // token有效时间
			ADMIN_STATUS: 1,
			ADMIN_TYPE: 1
		}
		// 使用静态方法调用getOne
		let admin = await AdminModel.getOne(where, 'ADMIN_ID,ADMIN_PHONE,ADMIN_NAME,ADMIN_TYPE');
		if (!admin)
			this.AppError('超级管理员不存在', appCode.ADMIN_ERROR);

		return admin;
	}

	/** 写入日志 */
	async insertLog(content, admin, type, level = LogModel.LEVEL.INFO, retention = LogModel.RETENTION.MEDIUM) {
		if (!admin) return;

		// 截断内容，避免过长
		if (content && content.length > 500) {
			content = content.substring(0, 497) + '...';
		}

		const now = timeUtil.time();
		let data = {
			LOG_CONTENT: content,
			LOG_ADMIN_ID: admin._id,
			LOG_ADMIN_NAME: admin.ADMIN_NAME,
			LOG_ADMIN_DESC: admin.ADMIN_DESC,
			LOG_TYPE: type,
			LOG_LEVEL: level,
			LOG_ADD_TIME: now,
			LOG_EDIT_TIME: now,
		}

		// 设置过期时间
		if (retention > 0) {
			data.LOG_EXPIRES = now + retention;
		}

		// 系统日志和高级别日志立即写入
		if (type === LogModel.TYPE.SYS || level >= LogModel.LEVEL.ERROR) {
			await LogModel.insert(data);
		} else {
			// 其他日志加入缓存队列
			await LogCache.add(data);
		}
	}

	/** 系统日志-信息级别 */
	async logSys(content) {
		await this.insertLog(content, this._admin, LogModel.TYPE.SYS, LogModel.LEVEL.INFO, LogModel.RETENTION.LONG);
	}

	/** 系统日志-警告级别 */
	async logSysWarn(content) {
		await this.insertLog(content, this._admin, LogModel.TYPE.SYS, LogModel.LEVEL.WARN, LogModel.RETENTION.LONG);
	}

	/** 系统日志-错误级别 */
	async logSysError(content) {
		await this.insertLog(content, this._admin, LogModel.TYPE.SYS, LogModel.LEVEL.ERROR, LogModel.RETENTION.FOREVER);
	}

	/** 用户日志-信息级别 */
	async logUser(content) {
		await this.insertLog(content, this._admin, LogModel.TYPE.USER, LogModel.LEVEL.INFO, LogModel.RETENTION.MEDIUM);
	}

	/** 其他日志-信息级别 */
	async logOther(content) {
		await this.insertLog(content, this._admin, LogModel.TYPE.OTHER, LogModel.LEVEL.INFO, LogModel.RETENTION.SHORT);
	}

	/** 新闻日志-信息级别 */
	async logNews(content) {
		await this.insertLog(content, this._admin, LogModel.TYPE.NEWS, LogModel.LEVEL.INFO, LogModel.RETENTION.MEDIUM);
	}

}

module.exports = BaseAdminService;